	package lifeOfAFish.scenegraph.geom;

import java.io.File;

import javax.media.opengl.*;

import com.sun.opengl.util.texture.Texture;
import com.sun.opengl.util.texture.TextureIO;

import lifeOfAFish.scenegraph.*;
import lifeOfAFish.util.MeshLoader;

public class sgGeomRect extends sgGeometry {

	private float m_alpha = 1.0f;
	private sgVec3[] m_points = new sgVec3[4];
	private sgVec3 m_normal = null;
	private boolean m_created = false;
	private int m_displayList = -1;

	public sgGeomRect()
	{
	}
	
	public sgGeomRect(sgVec3[] p)
	{
		setPoints(p);
	}
	
	public sgGeomRect(sgVec3 p1, sgVec3 p2, sgVec3 p3, sgVec3 p4)
	{
		setPoints(p1, p2, p3, p4);
	}
	
	
	public void setAlpha(float a)
	{
		m_alpha = a;
	}
	
	public void setNormal(sgVec3 n){
		m_normal = n;
	}
	
	public void setPoints(sgVec3[] p){
		for (int i = 0; i < p.length; i++)
		{
			m_points[i] = p[i];
		}
	}
	
	public void setPoints(sgVec3 p1, sgVec3 p2, sgVec3 p3, sgVec3 p4)
	{
		sgVec3[] p = {p1, p2, p3, p4};
		this.setPoints(p);
	}
	
	// additional push - texture bit
	protected void push()
	{
		super.push();
		sgGlobals.gl.glPushAttrib(GL.GL_TEXTURE_BIT);
	}
	
	// additional pop
	protected void pop()
	{
		super.pop();
		sgGlobals.gl.glPopAttrib();
	}
	
	/**
	 * Render the model, optional binding texture if necessary
	 */
	protected void draw()
	{
		GL gl = sgGlobals.gl;
		if(!m_created) create();
		gl.glCallList(m_displayList);
		
	};
	
	
	private void create()
	{
		sgVec3 bottomLeft = m_points[0];
		sgVec3 bottomRight = m_points[1];
		sgVec3 topRight = m_points[2];
		sgVec3 topLeft = m_points[3];
		
		
		GL gl = sgGlobals.gl;
		
		m_displayList = gl.glGenLists(1);
		gl.glNewList(m_displayList, GL.GL_COMPILE);
		gl.glBegin(GL.GL_QUADS);		
			gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3fv(bottomLeft.getV(), 0);
			gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3fv(bottomRight.getV(), 0);
			gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3fv(topRight.getV(), 0);
			gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3fv(topLeft.getV(), 0);
			if(m_normal != null){
				System.out.println("setting normal");
				gl.glNormal3fv(m_normal.getV(), 0);
			}
		gl.glEnd();	
		gl.glEndList();
		
		
		/*// create the rectangle, depending
		float width = (bottomRight.getX()-bottomLeft.getY());
		float step = width/10.0f;
		for(float x = bottomLeft.getX(); x < bottomLeft.getX()+width; x += step ){
		
			gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3fv(bottomLeft.getV(), 0);
			gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3fv(bottomRight.getV(), 0);
			gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3fv(topRight.getV(), 0);
			gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3fv(topLeft.getV(), 0);
			if(m_normal != null){
				System.out.println("setting normal");
				gl.glNormal3fv(m_normal.getV(), 0);
			}
		
			if(width == 0) x += 1.0f;
			
		}*/
		
		m_created = true;
	}
	
	/**
	 * Return (local) bounding box
	 */
	public float[] getBoundingBox(){
		
		float minX = m_points[0].getX();
		float maxX = m_points[0].getX();
		float minY = m_points[0].getY();
		float maxY = m_points[0].getY();
		float minZ = m_points[0].getZ();
		float maxZ = m_points[0].getZ();
		for(int i = 1; i < m_points.length; i++)
		{
			if(m_points[i].getX() < minX) minX = m_points[i].getX();
			if(m_points[i].getX() < maxX) maxX = m_points[i].getX();
			if(m_points[i].getY() < minY) minY = m_points[i].getY();
			if(m_points[i].getY() < maxY) maxY = m_points[i].getY();
			if(m_points[i].getZ() < minZ) minZ = m_points[i].getZ();
			if(m_points[i].getZ() < maxZ) maxZ = m_points[i].getZ();
		}
		
		float[] ret = {minX, maxX, minY, maxY, minZ, maxZ};
		return ret;
		
	}

}
